/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package e.z.blog.back.com.impl;

import e.z.blog.back.com.ComVerfiyCodeApi;
import java.awt.Color;
import java.awt.Font;
import java.awt.image.BufferedImage;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpSession;
import org.nutz.ioc.loader.annotation.IocBean;
import z.h.w.jar.data.time.Attr;
import z.h.w.jar.data.time.Time;
import z.h.w.jar.kit.Roll;
import z.h.w.jar.kit.To;
import z.h.w.jar.kit.code.String2Code;

/**
 *
 * @author EnzoZhong 验证码生成和验证
 */
@IocBean
public class ComVerfiyCode implements ComVerfiyCodeApi {
        
        final String VERFIY_CODE_ANSWER = "_验证码的答案_";
        private Map<String , String> test = createTest();
        
        private Map<String , String> createTest () {
                Map<String , String> map = new HashMap();
                Time time = Time.born();
                
//                map.put( "今年什么生肖？" , time.tell( Attr.ZODIAC ) );
//                map.put( "现在是什么座？" , time.tell( Attr.HOROSCOPE ) );
//                map.put( "现在几月份？" , time.tell( Attr.MONTH ) );
//                map.put( "现在几年？" , time.tell( Attr.YEAR ) );
//                map.put( "现在什么季节？" , time.tell( Attr.SEASON ) );
                map.put( "How Old Are U??" , Time.howOldAmI().toString() );
                map.put( "Quanti Anni Hai??" , Time.howOldAmI().toString() );
                
                
                return map;
        }

        /**
         * 数据库中获取所有的domanda，从中随机一个出来生成验证码
         * <p/>
         * @param session < p/>
         * <p/>
         * @return
         */
        @Override
        public BufferedImage create ( HttpSession session ) {
                //Color[] colors, Integer rangRoutate, Integer[] fontSize, Font[] fonts, String string
                
                String domanda = Roll.roll( test );
                Integer size = 36;
                Color[] colors = Roll.rollColor( 100 );
                Integer[] sizes = To.objs2Array( size , Double.valueOf( size * 1.8 ).intValue() );
                String[] fontNames = new String[] { "Arial" , "Arial Black" ,
                                                    "Impact" , "Microsoft Sans Serif" ,
                                                    "Tahoma" , "Times New Roman" };
                List<Font> fontList = new LinkedList();
                for ( String font : fontNames ) {
                        fontList.add( new Font( font , Font.PLAIN , size ) );
                }
                String2Code s2i = new String2Code( colors , 68 , true , sizes , To.list2Array( fontList ) , domanda );
                s2i.drawNoise( size / 2 );
                session.setAttribute( VERFIY_CODE_ANSWER , test.get( domanda ) );
                return s2i.getImage();
        }

        /**
         * 从session中取出所记录的domanda对象，并和传入的answer来进行对比
         * <p/>
         * @param session answer
         * <p/>
         * @param answer < p/>
         * <p/>
         * @return
         */
        @Override
        public Boolean verfiy ( HttpSession session , String answer ) {
                String domanda = ( String ) session.getAttribute( VERFIY_CODE_ANSWER );
                if ( domanda == null ) {
                        return false;
                } else {
                        return domanda.equals( answer );
                }
        }
}
